package com.example.musicplayer.util;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.widget.RemoteViews;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import com.example.musicplayer.R;
import com.example.musicplayer.activity.HomeActivity;
import com.example.musicplayer.broadcastreceiver.NotificationReceiver;
import com.example.musicplayer.entity.Music;
import com.example.musicplayer.service.MusicService;


public class MusicNotification extends Notification {
    private static final String MEDIA_CHANNEL_ID = "MEDIA_ID"; // 媒体播放渠道id
    private static final String MEDIA_CHANNEL_NAME = "MEDIA";  //媒体播放渠道名称

    private Context context;
    private RemoteViews remoteViewsSmall;
    private RemoteViews remoteViewsLarge;
    private NotificationManager manager;
    private NotificationChannel channel;
    private Notification notification;
    private MusicService.MusicBinder mMusicService;
    private Music mMusic;
    public static final int NOTIFICATION_ID = 1;
    public static final String ACTION_PREVIOUS = "com.intent.ACTION.PREVIOUS";
    public static final String ACTION_START_OR_PAUSE = "com.intent.ACTION.START_OR_PAUSE";
    public static final String ACTION_NEXT = "com.intent.ACTON.NEXT";
    public static final String ACTION_LIST = "com.intent.ACTION.LIST";

    // 传递Context对象
    @RequiresApi(api = Build.VERSION_CODES.O)
    public MusicNotification(Context context) {
        this.context = context;
        remoteViewsSmall = new RemoteViews(context.getPackageName(), R.layout.notification_small);
        remoteViewsLarge = new RemoteViews(context.getPackageName(), R.layout.notification_large);
        manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        channel = new NotificationChannel(MEDIA_CHANNEL_ID, MEDIA_CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
        manager.createNotificationChannel(channel);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void createNotification() {
        mMusicService = MusicService.getMusicService();
        mMusic = mMusicService.getCurrentMusic();
        remoteViewsSmall.setTextViewText(R.id.notification_music_name, mMusic.getName());
        remoteViewsSmall.setTextViewText(R.id.notification_artist_name, mMusic.getArtist());
        remoteViewsSmall.setImageViewBitmap(R.id.notification_album_img, mMusic.getAlbumArt());

        remoteViewsLarge.setTextViewText(R.id.notification_music_name, mMusic.getName());
        remoteViewsLarge.setImageViewBitmap(R.id.notification_album_img, mMusic.getAlbumArt());
        remoteViewsLarge.setTextViewText(R.id.notification_artist_name, mMusic.getArtist());
        Intent previousIntent = new Intent(context, NotificationReceiver.class);
        previousIntent.setAction(ACTION_PREVIOUS);
        PendingIntent previousPendingIntent = PendingIntent.getBroadcast(context, 0, previousIntent, 0);
        remoteViewsSmall.setOnClickPendingIntent(R.id.notification_previous, previousPendingIntent);
        remoteViewsLarge.setOnClickPendingIntent(R.id.notification_previous, previousPendingIntent);

        Intent startOrPauseIntent = new Intent(context, NotificationReceiver.class);
        startOrPauseIntent.setAction(ACTION_START_OR_PAUSE);
        PendingIntent startOrPausePendingIntent = PendingIntent.getBroadcast(context, 0, startOrPauseIntent, 0);
        remoteViewsSmall.setOnClickPendingIntent(R.id.notification_start_or_pause, startOrPausePendingIntent);
        remoteViewsLarge.setOnClickPendingIntent(R.id.notification_start_or_pause, startOrPausePendingIntent);

        Intent nextIntent = new Intent(context, NotificationReceiver.class);
        nextIntent.setAction(ACTION_NEXT);
        PendingIntent nextPendingIntent = PendingIntent.getBroadcast(context, 0, nextIntent, 0);
        remoteViewsSmall.setOnClickPendingIntent(R.id.notification_next, nextPendingIntent);
        remoteViewsLarge.setOnClickPendingIntent(R.id.notification_next, nextPendingIntent);

        Intent listIntent = new Intent(context, NotificationReceiver.class);
        listIntent.setAction(ACTION_LIST);
        PendingIntent.getBroadcast(context, 0, listIntent, 0);

        Intent intent = new Intent(context, HomeActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        notification = new NotificationCompat.Builder(context, MEDIA_CHANNEL_ID)
                .setChannelId(MEDIA_CHANNEL_ID)
                .setContentTitle("PPMusic")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.app_icon_round)
                .setContentIntent(pendingIntent)
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                .setCustomContentView(remoteViewsSmall)
                .setCustomBigContentView(remoteViewsLarge)
                .setOngoing(true)
                .build();
    }

    /**
     * 启动或更新通知，如果通知为启动，则为启动通知，如果通知已启动，则为更新通知。
     *
     * @param id 标识通知的唯一id。
     */
    public void updateNotification(int id) {
        manager.notify(id, notification);
    }

    public RemoteViews getSmallRemoteViews() {
        return remoteViewsSmall;
    }

    public RemoteViews getLargeRemoteViews() {
        return remoteViewsLarge;
    }
}
